import moment from "moment/moment";

export default {
	data() {
		return {}
	},
	computed: {
		is_loading() {
			return !!this.$store.state.loading;
		},
		is_shadow() {
			return !!this.$store.state.shadow;
		},
		is_login() {
			// const routes = getCurrentPages();
			// const currentPage = `/${routes[routes.length - 1].route}`;
			// if (currentPage == '/pages/home/withoutIccid/index') {
			// 	return true;
			// } else {
			// 	return !!this.$store.state.login.openid;
			// }

			// const needLoginPage = []; // 需要登录的页面
			// if (needLoginPage.indexOf(currentPage) >= 0) {
			// 	return !!this.$store.state.login.token;
			// } else {
			// 	return true;
			// }

			return true;
		},
	},
	methods: {
		async doLogin() {
			const uniLoginRes = await new Promise((resolve, reject) => {
				uni.login({
					provider: "weixin",
					success: (res) => {
						resolve(res);
					},
					fail: (err) => {
						reject(err);
					}
				});
			});
			console.log("uniLoginRes = ", uniLoginRes);
			const wkhLoginRes = await this.$http.claim$api$wx_login({
				appid: this.$store.state.login.wxappid,
				js_code: uniLoginRes.code,
				iccid: this.iccid || '',
			});
			console.log("wkhLoginRes = ", wkhLoginRes);
			// wkhLoginRes.iccid = "89860623570005487970";
			if (wkhLoginRes && wkhLoginRes.userInfo && wkhLoginRes.userInfo.openid) {
				this.$store.commit("login/setLoginInfo", {
					...this.$store.state.login,
					provider: "weixin",
					openid: wkhLoginRes.userInfo.openid,
					unionid: '',
					session_key: wkhLoginRes.userInfo.session_key,
					token: '',
					user_id: '',
					username: '',
					nickname: '',
					avatar: '',
					iccid: this.iccid || '',
					order_id: (wkhLoginRes.order && wkhLoginRes.order.status > 0 && wkhLoginRes.order.id) ?
						wkhLoginRes.order.id : '',
				});
				return true;
			} else {
				return false;
			}
		},
		getScene() {
			const {
				scene
			} = wx.getLaunchOptionsSync(); // 1154: 朋友圈场景
			console.log("getLaunchOptionsSync scene = ", scene);
			return scene;
		},
		async showToast({
			mode,
			message,
			duration,
		}) {
			console.log("showToast ===> ");
			await this.$refs.zkView.$refs.toast.show({
				mode, // 0-文案；1-成功；2-失败
				message,
				duration,
			});
			// Demo:
			// await this.showToast({
			// 	message: '提示内容',
			// })
		},
		async showModal({
			title,
			content,
			confirmText,
			cancelText,
			showConfirm,
			showCancel
		}) {
			console.log("showModal ===> ");
			return await this.$refs.zkView.$refs.modal.show({
				title,
				content,
				confirmText,
				cancelText,
				showCancel,
				showConfirm
			});
			// Demo:
			// await this.showModal({
			// 	title: '温馨提示',
			// 	content: '您确认吗？',
			// })
		}
	}
}